<?php

namespace backend\modules\bonus\controllers;

use Yii;
use common\models\bonus\BonusDjLog;
use common\models\bonus\BonusDjMemberLog;
use common\models\bonus\BonusDj;
use common\models\bonus\Bonus;
use common\models\forms\CreditsLogForm;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use backend\controllers\BaseController;
use common\traits\MerchantCurd;
use common\models\base\SearchModel;
/**
 * BonusDjLogController implements the CRUD actions for BonusDjLog model.
 */
class BonusDjLogController extends BaseController
{
    public $modelClass = BonusDjLog::class;

    use MerchantCurd;
    /**
     * Lists all BonusDjLog models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new SearchModel([
            'model' => $this->modelClass,
            'scenario' => 'default',
            'partialMatchAttributes' => ['type'], // 模糊查询，注意 member_nickname 为关联表的别名 表名_字段
            'defaultOrder' => [
                'id' => SORT_DESC
            ],
            'pageSize' => $this->pageSize
        ]);

        $dataProvider = $searchModel
            ->search(Yii::$app->request->queryParams);
            
        $list = BonusDj::find()->select(['sum(bonus_1) as bonus_1,sum(bonus_2) as bonus_2,sum(bonus_3) as bonus_3,sum(bonus_4) as bonus_4,sum(bonus_5) as bonus_5'])->asArray()->one();
        return $this->render('index', [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,
            'list' => $list,
        ]);
    }

    public function actionEditSf($str)
    {
        
        $transaction = Yii::$app->db->beginTransaction();
        try {
            if (!BonusDj::jieDong($str)) {
                throw new \Exception('释放失败，可能当前没有冻结的奖金', 1);
            }
            $transaction->commit();
            return $this->message('奖金已发放', $this->redirect(Yii::$app->request->referrer), 'success');
        } catch (\Exception $e) {
            $transaction->rollBack();
            return $this->message($e->getMessage(), $this->redirect(Yii::$app->request->referrer), 'error');
        }
    }
    public function actionMemberLog($id)
    {
        $searchModel = new SearchModel([
            'model' => BonusDjMemberLog::class,
            'scenario' => 'default',
            'relations' => ['member' => ['username']], // 关联 member 表的 nickname 字段
            'partialMatchAttributes' => ['code', 'member.username'], // 模糊查询，注意 member_nickname 为关联表的别名 表名_字段
            'defaultOrder' => [
                'id' => SORT_DESC
            ],
            'pageSize' => $this->pageSize
        ]);

        $dataProvider = $searchModel
            ->search(Yii::$app->request->queryParams);
        $dataProvider->query->andWhere(['{{%bonus_dj_member_log}}.pid'=>$id]);

        return $this->render($this->action->id, [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,
        ]);
    }
    protected function findModel($id)
    {
        if (($model = BonusDjLog::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
    }
}
